var db = require('../db').get(), utils = require('../utils');
var imageCl = db.image;

async function testTime(fn, name) {
    console.log('start test time', name);
    console.time(name)
    try {
        await fn();    
    } catch(e) {
        console.log(e);
    }
    console.timeEnd(name);
}
async function init() {
    var c = await imageCl.countDocuments({}), i = 23528 || Math.floor(Math.random() * c);
    var img = (await imageCl.find({}).skip(i).limit(1))[0];
    var distanceOf = utils.distanceOf;
    console.log(i, '--', c, 'img', img._id, img.baseInfo.src);
    await testTime(async function() {
        var rs = await imageCl.similaryImageIds(img._id, 8, ['5be1e6b116c4300cbc4eb6b3', '5be1e71316c4300cbc4ebc79', '5be1e94d16c4300cbc4eddab', '5be1e94d16c4300cbc4eddae']);
        rs
        .forEach((x, i) => {
            console.log('\tresult:', i, x._id, x.baseInfo.src, utils.distanceOf(x.baseInfo.aHash_8, img.baseInfo.aHash_8), utils.distanceOf(x.baseInfo.bHash_8, img.baseInfo.bHash_8));
        })
    }, 'query_hash8_d_with_bounded_ids');
    await testTime(async function() {
        var rs = await imageCl.similaryImageIds(img._id, 8);
        rs
        .forEach((x, i) => {
            console.log('\tresult:', i, x._id, x.baseInfo.src, utils.distanceOf(x.baseInfo.aHash_8, img.baseInfo.aHash_8), utils.distanceOf(x.baseInfo.bHash_8, img.baseInfo.bHash_8));
        })
    }, 'query_hash8_d_without_bounded_ids');
    
}

init().then(() => {
    process.exit();
});
